package com.springboot.test.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.test.common.utils.ConvertUtils;
import com.springboot.test.common.utils.DateTimeUtil;
import com.springboot.test.entity.VisitRecordEntity;
import com.springboot.test.entity.mapper.VisitRecordMapper;
import com.springboot.test.service.param.pageparam.BasePageParam;
import com.springboot.test.service.result.GoodsPageResult;
import com.springboot.test.service.result.VisitRecordResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class VisitRecordService extends ServiceImpl<VisitRecordMapper, VisitRecordEntity> {

    @Resource
    private GoodsService goodsService;

    public Boolean addVisitRecord(String goodsCode,String userCode){
        VisitRecordEntity recordEntity = getOne(new QueryWrapper<VisitRecordEntity>().lambda()
                .eq(VisitRecordEntity::getGoodsCode, goodsCode)
                .eq(VisitRecordEntity::getUserCode, userCode));
        if(recordEntity == null){
            recordEntity = new VisitRecordEntity();
            recordEntity.setGoodsCode(goodsCode);
            recordEntity.setUserCode(userCode);
        }
        recordEntity.setLastVisitTime(new Date());
        return saveOrUpdate(recordEntity);
    }

    public Page<VisitRecordResult> recordPage(BasePageParam param, String userCode) {
        Page<VisitRecordEntity> page = page(new Page<>(param.getPageNum(), param.getPageSize()), new QueryWrapper<VisitRecordEntity>().lambda()
                .eq(VisitRecordEntity::getUserCode, userCode)
                .orderByDesc(VisitRecordEntity::getLastVisitTime)
        );
        if(CollectionUtils.isEmpty(page.getRecords())){
            return new Page<>(param.getPageNum(),param.getPageSize());
        }
        List<VisitRecordEntity> records = page.getRecords();
        List<String> goodsCodes = records.stream().map(VisitRecordEntity::getGoodsCode).collect(Collectors.toList());
        List<GoodsPageResult> resultList = goodsService.listByGoodsCodes(goodsCodes);
        Map<String, GoodsPageResult> pageResultMap = resultList.stream().collect(Collectors.toMap(GoodsPageResult::getGoodsCode, a -> a));
        List<VisitRecordResult> recordResults = new ArrayList<>();
        for (VisitRecordEntity record : records) {
            if(pageResultMap.get(record.getGoodsCode()) != null){
                GoodsPageResult goodsPageResult = pageResultMap.get(record.getGoodsCode());
                VisitRecordResult result = ConvertUtils.convert(goodsPageResult, VisitRecordResult.class);
                result.setLastVisitTime(DateTimeUtil.Date2String(record.getLastVisitTime()));
                recordResults.add(result);
            }
        }
        Page<VisitRecordResult> pageResultPage = new Page<>();
        pageResultPage.setCurrent(page.getCurrent());
        pageResultPage.setSize(page.getSize());
        pageResultPage.setTotal(page.getTotal());
        pageResultPage.setRecords(recordResults);
        return pageResultPage;
    }
}
